package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @RequestMapping("/detail")
    public Object detail(String id){
        Strategy strategy = strategyService.get(id);
        return JsonResult.success(strategy);
    }

    @RequestMapping("/query")
    public Object query(StrategyQuery qo){
        Page<Strategy> list =  strategyService.query(qo);
        return JsonResult.success(list);
    }

    @RequireLogin
    @RequestMapping("/addComment")
    public Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo){
//        strategyComment.setNickname(userInfo.getNickname());
        //把userinfo和strategyComment中同名的属性进行拷贝
        //根据上面的功能，userinfo中的id会被拷贝到strategyComment中的id中
        //实际上我们需要把userinfo中的id拷贝到stragtegyComment中的userId中，并且stragtegyComment中的id应该为null
        //造成strategyComment中的id属性有了新的值（在service中矫正为null），并且userId中没有值
        BeanUtils.copyProperties(userInfo, strategyComment);
        strategyComment.setUserId(userInfo.getId());

        strategyCommentService.save(strategyComment);

        return JsonResult.success();
    }

    @RequestMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        Page<StrategyComment> page  =  strategyCommentService.query(qo);
        return JsonResult.success(page);
    }


}
